var qestore=[];
var qeicons=[];
var qeauth='';
var qeposttd=3;

var rtpth=location.href.match(/(http:\/\/.*?\/)index\.php/)[1];

function createXMLRequest()
{
if( window.XMLHttpRequest )
{
return new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
return new ActiveXObject("Msxml2.XMLHTTP");
}
catch( e )
{
try
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch( e )
{
return false;
}
}
}
}


function encq(d)
{
var nd='';
for(var i=0;i<d.length;i++)
{
var cc=d.charCodeAt(i);
if(cc>127)
nd+='&#'+cc+';';
else 
nd+=d.charAt(i)
}
return encodeURIComponent?encodeURIComponent(nd):escape(nd)
}

function qea(fid,tid,pid,buttonObj)
{
if(qestore[pid])return;

var pref=null;
var lnks=document.anchors;
for(var i=0;i<lnks.length;i++)
{
if(lnks[i].getAttribute('name')=='entry'+pid)
{
pref=lnks[i];
break
}
}

var parearef=pref.nextSibling;
if(parearef.nodeType!=1)parearef=parearef.nextSibling;
parearef=parearef.getElementsByTagName('td')[qeposttd];
parearef.id="posta"+pid;

qestore[pid]=parearef.innerHTML;
parearef.innerHTML='<form method="POST" action="#">'+
'<div class="quickedit"><div id="poste'+pid+'" class="editor"><textarea style="height:200px" disabled="disabled">Please wait as the post loads...</textarea></div>'+
'<div style="float:left" class="buttons"><input class="save" id="qesb'+pid+'" type="button" value="Save Changes" disabled="disabled" onclick="qed('+fid+','+tid+','+pid+')" /> '+
'<input id="qecb'+pid+'" class="cancel" type="button" disabled="disabled" value="Cancel" onclick="if(confirm(\'Are you sure you wish to discard your changes?\'))qec('+pid+')" /> '+
'<input id="qeab'+pid+'" class="full" type="button" onclick="this.form.submit()" disabled="disabled" value="Full Edit &gt;&gt;" /></div>'+
'<div style="text-align:right" class="right-buttons"><input type="button" value="+" onclick="if(this.form.Post)this.form.Post.style.height=parseInt(this.form.Post.style.height)+100+\'px\'" /><input type="button" value="-" onclick="if(this.form.Post)this.form.Post.style.height=parseInt(this.form.Post.style.height)-100+\'px\'" /></div>'+
'</div></form>';

var thd=createXMLRequest();
thd.open('GET',rtpth+'index.php?act=Post&CODE=08&f='+fid+'&t='+tid+'&p='+pid,true);
thd.onreadystatechange = function(){if(thd.readyState==4){qes(fid,tid,pid,thd)}};
thd.send(null);
}

function qes(fid,tid,pid,thd)
{
var earea=document.getElementById('poste'+pid);
var c=thd.responseText.match(/<textarea cols='80' rows='20' name='Post' tabindex='3' class='textinput'>((.|\n)*)<\/textarea>/)[1];
if(!c)c=thd.responseText.match(/<textarea cols='80' rows='20' name='Post' tabindex='3' class='textinput'>(.*)<\/textarea>/)[1];
qeauth=thd.responseText.match(/auth_key' value='(.{32})'/)[1];
qeicons[pid]=thd.responseText.match(/name=('|")iconid('|") value=('|")([0-9]+)('|") checked/)[4];
earea.innerHTML='<textarea style="height:200px" id="postc'+pid+'" name="Post">'+c+'</textarea>';
document.getElementById('postc'+pid).focus();
document.getElementById('qesb'+pid).disabled=false;
document.getElementById('qecb'+pid).disabled=false;
document.getElementById('qeab'+pid).disabled=false;
document.getElementById('postc'+pid).form.action=window.location.href.replace(/index\..*$/,'')+'index.php?act=Post&CODE=08&f='+fid+'&t='+tid+'&p='+pid+'&editupload=keep&add_edit=1&iconid='+qeicons[pid]+'&enableemo=yes&enablesig=yes&preview=1';
}

function qec(pid)
{
pa=document.getElementById('posta'+pid);
pa.innerHTML=qestore[pid];
qestore[pid]=null;
}

function qed(fid,tid,pid)
{
pa=document.getElementById('postc'+pid);
if(pa.value==''){alert('Please enter a post!');return}
document.getElementById('qesb'+pid).value="Saving... please wait";
try{var st=window.location.href.match(/st=([0-9]+)/)[1]}catch(e){var st=0}
var thd=createXMLRequest();
thd.open('POST',rtpth+'index.php?act=Post&CODE=09&f='+fid+'&t='+tid+'&p='+pid+'&st='+st+'&auth_key='+qeauth,true);
thd.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
thd.onreadystatechange = function(){if(thd.readyState==4){qel(pid,thd.responseText)}};
thd.send('editupload=keep&add_edit=1&iconid='+qeicons[pid]+'&enableemo=yes&enablesig=yes&Post='+encq(pa.value));
}

function qel(pid,c)
{
var r=new RegExp('<!\-\- THE POST '+pid+' \-\->((.|\n)*?)<!\-\- THE POST \-\->');
var c=r.exec(c)[1];
var pa=document.getElementById('posta'+pid);
pa.innerHTML=c;
qestore[pid]=null;
}

function qesetup()
{
if(!createXMLRequest)return;
if(!document.getElementById)return;
if(!document.body.innerHTML)return;
var lnks=document.links;
for(var i=0;i<lnks.length;i++)
{
if(lnks[i].href.match(/act=Post&CODE=08/))
{
var fid=lnks[i].href.match(/f=([0-9]+)/)[1];
var tid=lnks[i].href.match(/t=([0-9]+)/)[1];
var pid=lnks[i].href.match(/p=([0-9]+)/)[1];
var b=document.createElement('span');
b.innerHTML='<a style="cursor:pointer" onclick="qea('+fid+','+tid+','+pid+',this)">'+qebutton+'</a>';
lnks[i].parentNode.insertBefore(b,lnks[i].nextSibling);
}
}
}